package com.ymatou.database.mapper;

import com.ymatou.database.model.Account;
import com.ymatou.database.model.AccountEntry;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface AccountMapper {
	@Select("select  * from account  where  AccountId=#{accountId} ")
	List<Account> selectAccountInfo(@Param("accountId") String accountId);

	@Select("<script>" + "select * from account where userid=#{userId}"
			+ "<if test='accountType !=null '>"
			+ "and accounttype=#{accountType}" + "</if>"
			+ "order by UpdateTime desc" + "</script>")
	List<Account> selectAccountInfoByUserId(Map m);

	@Select("<script>"
			+ "SELECT * from account_entry where accountid=#{accountId}"
			+ "<if test='oNo !=null '>" + "and OriginalNo=#{oNo}" + "</if>"
			+ "order by updatetime desc" + "</script>")
	List<AccountEntry> selectAccountEntry(Map m);

	@Select("<script>"
			+ "select * from account_entry a,account b where a.accountid=b.accountid and b.userid=#{UserId}"
			+ "<if test='AccountType !=null '>"
			+ "and b.AccountType=#{AccountType}" + "</if>"
			+ "<if test='CurrencyType !=null '>"
			+ "and b.CurrencyType=#{CurrencyType}" + "</if>"
			+ "<if test='Entrytype !=null '>" + "and a.entrytype=#{Entrytype}"
			+ "</if>"
			+ "<if test='AccountingType !=null and AccountingType &gt;0'>"
			+ "and a.Amount &gt; 0" + "</if>"
			+ "<if test='AccountingType !=null and AccountingType &lt;0'>"
			+ "and a.Amount &lt; 0" + "</if>" + "<if test='BeginTime !=null '>"
			+ "and a.CreateTime &gt;  #{BeginTime}" + "</if>"
			+ "<if test='EndTime !=null '>"
			+ "and a.CreateTime &lt; #{EndTime}" + "</if>"
			+ "order by a.UpdateTime" + "</script>")
	List<AccountEntry> selectAccountEntryAndInfo2(Map m);

	@Select("select * from account_entry where UserId=#{userId} and BizNo=#{bizNo} and "
			+ "accountid in  (select accountid from account where userid=#{userId} and accounttype=#{accountType})")
	List<AccountEntry> selectAccountEntryByUserid(Map m);

	@Select("select  * from account_entry a,account b where a.accountid=b.AccountId order by a.updatetime desc limit 10")
	List<HashMap> selectAccountEntryAndInfo();

	@Select(" select  * from account_entry where UserId=#{userId} order by updatetime desc limit 1")
	List<AccountEntry> selectAccountEntryByUserid2(
            @Param("userId") Integer userId);

	@Select(" select   * from account_entry where UserId=#{userId} order by	updatetime desc limit #{limit}")
	List<AccountEntry> selectAccountEntryByUseridLimit(Map m);

	@Select(" select count(*) as count from account_entry where UserId=#{userId}")
	List<HashMap> selectCountByUserid(@Param("userId") Integer userId);

	@Select("<script>" + "select * from account_entry" + "<where>"
			+ "<if test='bizno !=null '>" + "bizno=#{bizno}	" + "</if>"
			+ "<if test='originalNo !=null '>"
			+ " and OriginalNo=#{originalNo}" + "</if>"
			+ "<if test='userId !=null '>" + " and UserId=#{userId}" + "</if>"
			+ "<if test='bizcode !=null '>" + " and BizCode=#{bizcode}" + "</if>"
			+ "</where> order by entryid desc" + "</script>")
	List<AccountEntry> selectAccountEntryByBizNo(Map m);

	@Select("select * from account_entry where OriginalNo=#{originalNo}")
	List<AccountEntry> selectAccountEntryByOriginalNo(
            @Param("originalNo") String originalNo);

	@Select("select * from account_entry where accountid=#{accountId} order by entryid desc")
	List<AccountEntry> selectAccountEntryByAcId(
            @Param("accountId") String accountId);

	@Select("<script>"
			+ "select "
			+ "<if test='limit !=null '> "
			+ "top ${limit} "
			+ "</if> "
			+ "* from account_entry  "
			+ "<where> "
			+ "<if test='userId !=null '> "
			+ "UserId=#{userId} "
			+ "</if> "
			+ "<if test='bizno !=null '> "
			+ "and bizno=#{bizno} "
			+ "</if> "
			+ "<if test='originalNo !=null '> "
			+ "and OriginalNo=#{originalNo} "
			+ "</if> "
			+ "<if test='bizcode !=null '> "
			+ "and bizcode=#{bizcode} "
			+ "</if> "
			+ "<if test='entryType !=null '> "
			+ "and entryType=#{entryType} "
			+ "</if> "
			+ "<if test='entryid !=null '> "
			+ "and entryid=#{entryid} "
			+ "</if> "
			+ "<if test='accountingType !=null and accountingType.equals(1)'> "
			+ "<![CDATA[ and amount > 0 ]]>   "
			+ "</if> "
			+ "<if test='accountingType !=null and accountingType.equals(-1)'> "
			+ "<![CDATA[ and amount < 0 ]]>   "
			+ "</if> "
			+ "<if test='accountTypeList !=null'> "
			+ "and accountid in (select AccountId from account  where userid=#{userId} and AccountType in "
			+ "<foreach item='item' index='index' collection='accountTypeList' open='(' separator=',' close=')'>   "
			+ "#{item}   " + "</foreach> " + ") " + "</if> " + "</where> "
			+ "order by entryid desc " + "</script>")
	List<AccountEntry> selectAccountEntryByMap(Map m);
}